<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );


class PimModelImport extends JModel {
	
	function __construct() {
		parent::__construct();
	}
	
	function importSerial($file_name) {
		$objReader = new PHPExcel_Reader_Excel5;
		$objPHPExcel = $objReader->load($file_name);
		$objWorksheet = $objPHPExcel->getActiveSheet();
		
		$highestRow = $objWorksheet->getHighestRow();
		$highestColumn = $objWorksheet->getHighestColumn();
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
		
		if ($highestColumnIndex == 1 && $highestRow == 1) { // no data in import file
			return false;
		} else {
			$model_serial = JModel::getInstance(Serial, 'PimModel');
			
			$order = 1;
			$max_batch = $model_serial->getMaxBatch();
        	
			for ($row = ManagerHelper::ROW_START_IMPORT_EXCEL + 1; $row <= $highestRow; ++$row) {
				$table_serial = & JTable::getInstance('Serial', 'Table');
				$arr_data = array();
				
				if ($order == ManagerHelper::NUM_OF_BATCH + 1) {
					$order = 1;
					$max_batch++;
				}
				
				$encrypted_code = trim($objWorksheet->getCell('B' . $row)->getValue());
				
				$arr_data['code']= trim($objWorksheet->getCell('A' . $row)->getValue());
				$arr_data['encrypted_code'] = md5($encrypted_code);
				$arr_data['generated_date']= date("Y-m-d H:i:s");
				$arr_data['order']= $order;
				$arr_data['batch']= $max_batch;
				$arr_data['status']= ManagerHelper::SERIAL_STATUS_NOT_USE;
				
				if ($table_serial->save($arr_data)) {
					$order++;
				} else {
					return false;
				}
			}
			
			return true;
		}
	}
	
	function importProduct($file_name) {
		$objReader = new PHPExcel_Reader_Excel5;
		$objPHPExcel = $objReader->load($file_name); 
		$objWorksheet = $objPHPExcel->getActiveSheet();
		
		$highestRow = $objWorksheet->getHighestRow();
		$highestColumn = $objWorksheet->getHighestColumn();
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
		
		if ($highestColumnIndex == 1 && $highestRow == 1) { // no data in import file
			JError::raiseWarning('', "File has no data, please try again!!!");
			return false;
		} else {
			$model_config = JModel::getInstance('Config', 'ManagerModel');
			$model_serial = JModel::getInstance('Serial', 'PimModel');
			$model_pond = JModel::getInstance('Pond', 'PimModel');
			$model_fishclass = JModel::getInstance('FishClass', 'PimModel');
			$model_customer = JModel::getInstance('Customer', 'PimModel');
			
        	$obj_config = $model_config->getConfigByKey("duration_expired");        
        	$num_expired_value = intval($obj_config->pim_value);
        	
        	$arr_serial = $model_serial->getListSerialCode();
        	$arr_pond = $model_pond->getListPondCode();
        	$arr_fishclass = $model_fishclass->getListFishClassCode();
        	$arr_customer = $model_customer->getListCustomerCode();
        	
			for ($row = ManagerHelper::ROW_START_IMPORT_EXCEL+1; $row <= $highestRow; ++$row) {
				$table_product = & JTable::getInstance('Product', 'Table');
				$arr_data = array();
				
				$serial_code = trim($objWorksheet->getCell('A' . $row)->getValue());
				$pond_code = trim($objWorksheet->getCell('B' . $row)->getValue());
				$class_code = trim($objWorksheet->getCell('C' . $row)->getValue());
				$customer_code = trim($objWorksheet->getCell('D' . $row)->getValue());
				
				if (!in_array($serial_code, $arr_serial)) {
					JError::raiseWarning('', "Serial Code in row $row does not exist, please try again!!!");
					return false;
				}
				
				if (!in_array($pond_code, $arr_pond)) {
					JError::raiseWarning('', "Pond Code in row $row does not exist, please try again!!!");
					return false;
				}
				
				if (!in_array($class_code, $arr_fishclass)) {
					JError::raiseWarning('', "Fish Class Code in row $row does not exist, please try again!!!");
					return false;
				}
				
				if (!in_array($customer_code, $arr_customer)) {
					JError::raiseWarning('', "Customer Code in row $row does not exist, please try again!!!");
					return false;
				}
				
				$feed_date = trim($objWorksheet->getCell('E' . $row)->getValue());
				$feed_date = date("Y-m-d H:i:s", PHPExcel_Shared_Date::ExcelToPHP($feed_date));
				$sale_date = trim($objWorksheet->getCell('F' . $row)->getValue());
				$sale_date = date("Y-m-d H:i:s", PHPExcel_Shared_Date::ExcelToPHP($sale_date));
				$expired_date = ManagerHelper::getExpiredDate($sale_date, $num_expired_value);
				
				$arr_data['serial_code'] = $serial_code;
				$arr_data['pond_code'] = $pond_code;
				$arr_data['class_code'] = $class_code;
				$arr_data['customer_code'] = $customer_code;
				$arr_data['feed_date'] = $feed_date;
				$arr_data['sale_date'] = $sale_date;
				$arr_data['expired_date'] = $expired_date;
				$arr_data['status'] = ManagerHelper::PRODUCT_STATUS_CAN_USE;
				
				if ($table_product->save($arr_data)) {
					$model_serial->updateStatus($serial_code, ManagerHelper::SERIAL_STATUS_USED, ManagerHelper::SERIAL_TYPE_OPEN);
				} else {
					return false;
				}
			}
			
			return true;
		} 		
	}
}